package ua.in.iticket.addressbook.report;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import ua.in.iticket.addressbook.person.Person;
import ua.in.iticket.addressbook.person.PhoneNumber;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by ivanov-av
 * 11.02.14 11:19.
 */
public class OfficeReport implements Report {
    @Override
    public void TextReport(List<Person> list, String filePath) throws ReportException {
        XWPFDocument document = new XWPFDocument();
        Iterator<Person> iterator = list.iterator();
        while (iterator.hasNext()) {
            Person person = iterator.next();
            XWPFParagraph p1 = document.createParagraph();
            XWPFRun r1 = p1.createRun();

            r1.setText("Имя и фамилия (full name): ");
            r1.setText(person.getFullName());
            r1.addBreak();

            r1.setText("Возраст (age): ");
            r1.setText(String.valueOf(person.getAge()));
            r1.addBreak();

            r1.setText("Адрес (address): ");
            r1.setText(person.getAddress().getFullAdress());
            r1.addBreak();

            if (person.getPhoneNumbersSize() != 0) {
                if (person.getPhoneNumbersSize() == 1) {
                    r1.setText("Телефон (phone): ");
                    r1.setText(person.getPhoneNumber(0).getFullNumber());
                    r1.addBreak();
                } else {
                    r1.setText("Телефоны (phones): ");
                    r1.addBreak();
                    for (int i = 0; i < person.getPhoneNumbersSize(); i++) {
                        r1.setText("Телефон (phone): ");
                        r1.setText(person.getPhoneNumber(i).getFullNumber());
                        r1.addBreak();
                    }
                }
            }
            p1.setPageBreak(true);
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath);
        } catch (FileNotFoundException e) {
            throw new ReportException(e);
        }
        try {
            document.write(out);
        } catch (IOException e) {
            throw new ReportException(e);
        }
        try {
            out.close();
        } catch (IOException e) {
            throw new ReportException(e);
        }
    }

    @Override
    public void TableReport(List<Person> list, String filePath) throws ReportException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Person data");
        int rownum = 0;
        Row rowLabel = sheet.createRow(rownum++);
        Cell cellLabel = rowLabel.createCell(0);
        cellLabel.setCellValue("Имя и фамилия (full name)");
        cellLabel = rowLabel.createCell(1);
        cellLabel.setCellValue("Возраст (age)");
        cellLabel = rowLabel.createCell(2);
        cellLabel.setCellValue("Адрес (address)");
        cellLabel = rowLabel.createCell(3);
        cellLabel.setCellValue("Телефоны (phones)");
        Iterator<Person> iterator = list.iterator();
        while (iterator.hasNext()) {
            Person person = iterator.next();
            Row row = sheet.createRow(rownum++);
            int cellnum = 0;
            Cell cell = row.createCell(cellnum++);
            cell.setCellValue(person.getFullName());
            cell = row.createCell(cellnum++);
            cell.setCellValue(person.getAge());
            cell = row.createCell(cellnum++);
            cell.setCellValue(person.getAddress().getFullAdress());
            cell = row.createCell(cellnum++);
            CellStyle style = workbook.createCellStyle();
            style.setWrapText(true);
            cell.setCellStyle(style);
            String phones = "";
            for (int i = 0; i < person.getPhoneNumbersSize(); i++) {
                phones = new StringBuilder().append(phones).append(person.getPhoneNumber(i).getFullNumber()).append("\n").toString();
            }
            cell.setCellValue(phones);
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath);
        } catch (FileNotFoundException e) {
            throw new ReportException(e);
        }
        try {
            workbook.write(out);
        } catch (IOException e) {
            throw new ReportException(e);
        }
        try {
            out.close();
        } catch (IOException e) {
            throw new ReportException(e);
        }

    }
}
